package com.gs.netty.core.factory;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
     * 线程工厂
     */
public class NettyThreadFactory implements ThreadFactory {

    public   static   ThreadFactory webSocketThreadFactory(String threadNamePrefix){
        return new NettyThreadFactory(threadNamePrefix);
    }


    private static final AtomicInteger webSocketPoolNumber = new AtomicInteger(1);
    private final ThreadGroup group;
    private final AtomicInteger webSocketThreadNumber = new AtomicInteger(1);
    private final String namePrefix;

  public NettyThreadFactory(String threadNamePrefix) {
        SecurityManager s = System.getSecurityManager();
        group = (s != null) ? s.getThreadGroup() :
                Thread.currentThread().getThreadGroup();


        namePrefix = threadNamePrefix+ "-pool-" +
                webSocketPoolNumber.getAndIncrement() +
                "-thread-";
    }

    public Thread newThread(Runnable r) {
        Thread t = new Thread(group, r,
                namePrefix + webSocketThreadNumber.getAndIncrement(),
                0);
        if (t.isDaemon())
            t.setDaemon(false);
        if (t.getPriority() != Thread.NORM_PRIORITY)
            t.setPriority(Thread.NORM_PRIORITY);
        return t;
    }
}